#include "aht20.h"
#include "auto_light.h"
#include "fan.h"
#include "feeling.h"
#include "pwm_patch.h"
#include "sensor.h"
#include "wiper.h"
#include "seat.h"

#include "app_init.h"
#include "pinctrl.h"
#include "i2c.h"
#include "adc.h"
#include "adc_porting.h"
#include "osal_debug.h"
#include "soc_osal.h"
#include "watchdog.h"
#include "watchdog_porting.h"
#include "security_unified_porting.h"


static SensorControl g_sensor = {0};

SensorData sensor_data = {0};
// 子线程

void *Sensor_Task(void *arg)
{
    UNUSED(arg);
    while (1) {
        osal_kthread_lock();
        g_sensor = sensor_get();
        printf("[Sensor] auto_light=%d, fresh_fan=%d, breath_led=%d, seat_ctrl=%d, wiper_ctrl=%d\n", 
                  g_sensor.auto_light, g_sensor.fresh_fan, g_sensor.breath_led, g_sensor.seat_ctrl, g_sensor.wiper_ctrl);
        osal_kthread_unlock();
        
        // usleep(200000); // 200ms 更新间隔
        osDelay(1000);
    }
    return NULL;
}

// Fan_Task

void *FanTestTask(void *arg)
{
    UNUSED(arg);
    osal_printk("start fan test\r\n");
    int fresh_fan=0;
    uint32_t baudrate = AHT20_BAUDRATE;
    uint32_t hscode = AHT20_I2C_ADDR;
    TemperFanInit();
    errcode_t ret = uapi_i2c_master_init(1, baudrate, hscode);
    if (ret != 0) {
        printf("i2c init failed, ret = %0x\r\n", ret);
    }
    while(1){
        fresh_fan=g_sensor.fresh_fan;
        switch (fresh_fan) {
            case 0: // 自动模式
                AutoFreshFan();
                break;
            case 1: // 停止
                FreshFanStop();
                break;
            case 2: // 手动开启
                FreshFanOn();
                break;
            default:
                break;
        }
        osDelay(50);
    }
}

// Auto_Light

void *AutoTask(void *arg) {
    (void)arg;
    AutoLightInit();
    printf("Auto Control Task Started\n");
    int autostate = 0;
    while (1) {
        // 根据全局状态执行控制逻辑
        autostate = g_sensor.auto_light;
       if(autostate == 0) {
            AutoLightTask();
        }
        // usleep(DELAY_US); // 每1秒检查一次
        osDelay(50);
    }
}

// Feeling_Task

void *FeelingTestTask(void *arg)
{
    UNUSED(arg);
    osal_printk("start feeling test\r\n");
    PWMLedInit();
    int breath = 4;
    while(1) {
        breath = g_sensor.breath_led;
        PWMLed(breath);
        osDelay(50);
    }
    osal_printk("end feeling test\r\n");
}

// Wiper_Task

void *WiperTestTask(void *arg)
{
    UNUSED(arg);
    osal_printk("start wiper test\r\n");
    WiperInit();
    AutoWiperStart();
    // ManualWiperControl();
    osal_printk("end wiper test\r\n");
    return NULL;
}

// Seat_Task

// void *Seat_Task(void *arg)
// {
//     UNUSED(arg);
//     osal_printk("start seat test\r\n");
//     SeatCtrlInit();
//     osal_printk("init successed\r\n");
//     int seat_ctrl = 1;
//     int last_ctrl = -1;
//     while (1) {
//         // uapi_watchdog_kick();
//         seat_ctrl = g_sensor.seat_ctrl;
//         if (seat_ctrl != last_ctrl) {
//             switch (seat_ctrl) {
//                 case 1:
//                     sitting();
//                     break;
//                 case 2:
//                     lying();
//                     break;
//                 case 3:
//                     sleeping();
//                     break;
//                 case 0:
//                     SeatReset();
//                     break;
//                 default:
//                     break;
//             }
//         }
//         last_ctrl = seat_ctrl;
//     }
//     osDelay(10);
// }